package com.ikas.ai.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A pure java base64 encoder and decoder, based on source code of
 * com.sun.org.apache.xerces.internal.impl.dv.util.Base64.
 */
public final class Base64Util {
    private static Logger logger = LoggerFactory.getLogger(Base64Util.class);

    private Base64Util() {

    }

    public static String encodeString(String text) {
        return encode(text.getBytes());
    }

    /**
     * Decode and return original text. Invalid base64 content will return null.
     *
     * @param base64 要解密的字符串
     * @return
     */
    public static String decodeString(String base64) {
        return new String(decode(base64));
    }

    private static final int BASELENGTH = 255;

    private static final int LOOKUPLENGTH = 64;

    private static final int TWENTYFOURBITGROUP = 24;

    private static final int EIGHTBIT = 8;

    private static final int SIXTEENBIT = 16;

    private static final int FOURBYTE = 4;

    private static final int SIGN = -128;

    private static final char PAD = '=';

    private static final boolean FDEBUG = false;

    private static final byte[] BASE64ALPHABET = new byte[BASELENGTH];

    private static final char[] LOOKUPBASE64ALPHABET = new char[LOOKUPLENGTH];

    static {

        for (int i = 0; i < BASELENGTH; i++) {
            BASE64ALPHABET[i] = -1;
        }
        for (int i = 'Z'; i >= 'A'; i--) {
            BASE64ALPHABET[i] = (byte) (i - 'A');
        }
        for (int i = 'z'; i >= 'a'; i--) {
            BASE64ALPHABET[i] = (byte) (i - 'a' + 26);
        }

        for (int i = '9'; i >= '0'; i--) {
            BASE64ALPHABET[i] = (byte) (i - '0' + 52);
        }

        BASE64ALPHABET['+'] = 62;
        BASE64ALPHABET['/'] = 63;

        for (int i = 0; i <= 25; i++) {
            LOOKUPBASE64ALPHABET[i] = (char) ('A' + i);
        }
        for (int i = 26, j = 0; i <= 51; i++, j++) {
            LOOKUPBASE64ALPHABET[i] = (char) ('a' + j);
        }
        for (int i = 52, j = 0; i <= 61; i++, j++) {
            LOOKUPBASE64ALPHABET[i] = (char) ('0' + j);
        }
        LOOKUPBASE64ALPHABET[62] = '+';
        LOOKUPBASE64ALPHABET[63] = '/';

    }

    protected static boolean isWhiteSpace(char octect) {
        return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
    }

    protected static boolean isPad(char octect) {
        return (octect == PAD);
    }

    protected static boolean isData(char octect) {
        return (BASE64ALPHABET[octect] != -1);
    }

    protected static boolean isBase64(char octect) {
        return (isWhiteSpace(octect) || isPad(octect) || isData(octect));
    }

    /**
     * Encodes hex octects into Base64.
     *
     * @param binaryData Array containing binaryData
     * @return Encoded Base64 array
     */
    public static String encode(byte[] binaryData) {

        if (binaryData == null) {
            return null;
        }
        int lengthDataBits = binaryData.length * EIGHTBIT;
        if (lengthDataBits == 0) {
            return "";
        }

        int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
        int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
        int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets;
        int numberLines = (numberQuartet - 1) / 19 + 1;
        char[] encodedData = null;

        encodedData = new char[numberQuartet * 4 + numberLines];

        byte k = 0;
        byte l = 0;
        byte b1 = 0;
        byte b2 = 0;
        byte b3 = 0;

        int encodedIndex = 0;
        int dataIndex = 0;
        int i = 0;
        if (FDEBUG) {
            logger.debug("number of triplets = " + numberTriplets);
        }

        for (int line = 0; line < numberLines - 1; line++) {
            for (int quartet = 0; quartet < 19; quartet++) {
                b1 = binaryData[dataIndex++];
                b2 = binaryData[dataIndex++];
                b3 = binaryData[dataIndex++];

                if (FDEBUG) {
                    logger.debug("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3);
                }

                l = (byte) (b2 & 0x0f);
                k = (byte) (b1 & 0x03);

                byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);

                byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
                byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);

                if (FDEBUG) {
                    logger.debug("val2 = " + val2);
                    logger.debug("k4   = " + (k << 4));
                    logger.debug("vak  = " + ((val2 & 0xFF) | (k << 4)));
                }

                encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[val1];
                encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[(val2 & 0xFF) | (k << 4)];
                encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[(l << 2) | (val3 & 0xFF)];
                encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[b3 & 0x3f];

                i++;
            }
            encodedData[encodedIndex++] = 0xa;
        }

        for (; i < numberTriplets; i++) {
            b1 = binaryData[dataIndex++];
            b2 = binaryData[dataIndex++];
            b3 = binaryData[dataIndex++];

            if (FDEBUG) {
                logger.debug("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3);
            }

            l = (byte) (b2 & 0x0f);
            k = (byte) (b1 & 0x03);

            byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);

            byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
            byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);

            if (FDEBUG) {
                logger.debug("val2 = " + val2);
                logger.debug("k4   = " + (k << 4));
                logger.debug("vak  = " + ((val2 & 0xFF) | (k << 4)));
            }

            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[val1];
            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[(val2 & 0xFF) | (k << 4)];
            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[(l << 2) | (val3 & 0xFF)];
            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[b3 & 0x3f];
        }

        // form integral number of 6-bit groups
        if (fewerThan24bits == EIGHTBIT) {
            b1 = binaryData[dataIndex];
            k = (byte) (b1 & 0x03);
            if (FDEBUG) {
                logger.debug("b1=" + b1);
                logger.debug("b1<<2 = " + (b1 >> 2));
            }
            byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[val1];
            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[k << 4];
            encodedData[encodedIndex++] = PAD;
            encodedData[encodedIndex++] = PAD;
        } else if (fewerThan24bits == SIXTEENBIT) {
            b1 = binaryData[dataIndex];
            b2 = binaryData[dataIndex + 1];
            l = (byte) (b2 & 0x0f);
            k = (byte) (b1 & 0x03);

            byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
            byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);

            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[val1];
            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[(val2 & 0xFF) | (k << 4)];
            encodedData[encodedIndex++] = LOOKUPBASE64ALPHABET[l << 2];
            encodedData[encodedIndex++] = PAD;
        }

        encodedData[encodedIndex] = 0xa;

        return new String(encodedData);
    }

    /**
     * Decode and return original data. Invalid base64 content will return null.
     */
    public static byte[] decode(String encoded) {

        if (encoded == null) {
            return new byte[0];
        }
        char[] base64Data = encoded.toCharArray();
        // remove white spaces
        int len = removeWhiteSpace(base64Data);

        if (len % FOURBYTE != 0) {
            // should be divisible by four
            return new byte[0];
        }

        int numberQuadruple = (len / FOURBYTE);

        if (numberQuadruple == 0) {
            return new byte[0];
        }
        byte[] decodedData = null;
        byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
        char d1 = 0, d2 = 0, d3 = 0, d4 = 0;

        int i = 0;
        int encodedIndex = 0;
        int dataIndex = 0;
        decodedData = new byte[(numberQuadruple) * 3];

        for (; i < numberQuadruple - 1; i++) {
            d1 = base64Data[dataIndex++];
            d2 = base64Data[dataIndex++];
            d3 = base64Data[dataIndex++];
            d4 = base64Data[dataIndex++];
            if (!isData(d1) || !isData(d2) || !isData(d3) || !isData(d4)) {
                // if found "no data" just return null
                return new byte[0];
            }
            b1 = BASE64ALPHABET[d1];
            b2 = BASE64ALPHABET[d2];
            b3 = BASE64ALPHABET[d3];
            b4 = BASE64ALPHABET[d4];

            decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
            decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
            decodedData[encodedIndex++] = (byte) (b3 << 6 | (b4 & 0xFF));
        }

        d1 = base64Data[dataIndex++];
        d2 = base64Data[dataIndex++];
        if (!isData(d1) || !isData(d2)) {
            // if found "no data" just return null
            return new byte[0];
        }

        b1 = BASE64ALPHABET[d1];
        b2 = BASE64ALPHABET[d2];

        d3 = base64Data[dataIndex++];
        d4 = base64Data[dataIndex++];
        if (!isData((d3)) || !isData((d4))) {
            // Check if they are PAD characters
            if (isPad(d3) && isPad(d4)) {
                // Two PAD e.g.
                // 3c[Pad][Pad]
                if ((b2 & 0xf) != 0) {
                    // last 4 bits should be zero
                    return new byte[0];
                }
                byte[] tmp = new byte[i * 3 + 1];
                System.arraycopy(decodedData, 0, tmp, 0, i * 3);
                tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
                return tmp;
            } else if (!isPad(d3) && isPad(d4)) { // One
                // PAD
                // e.g.
                // 3cQ[Pad]
                b3 = BASE64ALPHABET[d3];
                if ((b3 & 0x3) != 0) {
                    // last 2 bits should be zero
                    return new byte[0];
                }
                byte[] tmp = new byte[i * 3 + 2];
                System.arraycopy(decodedData, 0, tmp, 0, i * 3);
                tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
                tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
                return tmp;
            } else {
                // an error like "3c[Pad]r", "3cdX", "3cXd",
                return new byte[0];
                // "3cXX" where X is non data
            }
        } else { // No PAD e.g 3cQl
            b3 = BASE64ALPHABET[d3];
            b4 = BASE64ALPHABET[d4];
            decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
            decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
            decodedData[encodedIndex++] = (byte) (b3 << 6 | (b4 & 0xFF));

        }

        return decodedData;
    }

    /**
     * remove WhiteSpace from MIME containing encoded Base64 data.
     *
     * @param data the byte array of base64 data (with WS)
     * @return the new length
     */
    protected static int removeWhiteSpace(final char[] data) {
        if (data == null) {
            return 0;
        }
        // count characters that's not whitespace
        int newSize = 0;
        int len = data.length;
        for (int i = 0; i < len; i++) {
            if (!isWhiteSpace(data[i])) {
                data[newSize++] = data[i];
            }
        }
        return newSize;
    }
}
